package liujia.bt.display.base
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.MovieClip;
	import flash.events.MouseEvent;
	import flash.filters.BitmapFilter;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.Dictionary;
	
	import liujia.bt.display.BtDisplay;
	import liujia.bt.display.base.vo.ListenerVO;
	import liujia.bt.display.core.BtAnalyse;
	import liujia.bt.display.core.BtFrame;
	import liujia.bt.display.core.BtRun;
	import liujia.bt.display.interf.IBtAction;
	import liujia.bt.display.interf.IBtFrameRate;
	import liujia.bt.display.interf.IBtMouse;
	import liujia.bt.display.interf.IBtTransform;
	
	
	[Event(name="click",type="flash.events.MouseEvent")]
	[Event(name="doubleClick",type="flash.events.MouseEvent")]
	[Event(name="mouseMove",type="flash.events.MouseEvent")]
	[Event(name="mouseDown",type="flash.events.MouseEvent")]
	[Event(name="mouseUp",type="flash.events.MouseEvent")]
	[Event(name="mouseOut",type="flash.events.MouseEvent")]
	[Event(name="mouseOver",type="flash.events.MouseEvent")]
	[Event(name="mouseWheel",type="flash.events.MouseEvent")]
	[Event(name="rollOut",type="flash.events.MouseEvent")]
	[Event(name="rollOver",type="flash.events.MouseEvent")]
	
	/**
	 * BtAnim
	 * @author 刘佳 liujia08@snda.com
	 * $Id: BtAnim.as 2 2011-10-31 01:46:19Z sigmaliu@163.com $
	 * @version 1.0
	 */
	public class BtAnim extends BtAnimLite implements IBtAction, IBtFrameRate, IBtMouse
	{
		public var action2FrameDic:Dictionary = new Dictionary();
		
		public var currentAction:String = "";
		
		public var currentFrameArr:Array = null;
		
		public var isPlayOnce:Boolean = false;
		
		public var lastActionFrame:int = 0;
		
		public var actionFinishListener:Function = null;
		
		public var listenerParam:Object = null;
		
		protected var _mouseEnable:Boolean = true;
		public function get mouseEnable():Boolean {
			return _mouseEnable;	
		}
		public function set mouseEnable(value:Boolean):void {
			_mouseEnable = value;
		}
		
		protected var _doubleClickEnabled:Boolean = true;
		public function get doubleClickEnabled():Boolean {
			return _doubleClickEnabled;	
		}
		public function set doubleClickEnabled(value:Boolean):void {
			_doubleClickEnabled = value;
		}
		
		protected var isDrag:Boolean = false;
		protected var isLockCenter:Boolean = false;
		public function startDrag(lockCenter:Boolean = false, bounds:Rectangle = null):void {
			isDrag = true;
			isLockCenter = lockCenter;
		}
		public function stopDrag():void {
			isDrag = false;
		}
		
		protected var _frameRate:Number = 0;
		protected var _ms:Number = 0;
		public function set frameRate(value:Number):void {
			_frameRate = value;
			_ms = uint(1000/_frameRate);
		}
		public function get frameRate():Number {
			return _frameRate;
		}
		
		
		public function BtAnim(bitmapData:BitmapData=null, pixelSnapping:String="auto", smoothing:Boolean=false)
		{
			super(bitmapData, pixelSnapping, smoothing);
			_frameRate = BtRun.stage.frameRate;
		}
		
		protected var oldHit:Boolean = false;
		protected var nowHit:Boolean = false;
		protected var mouseEvent:MouseEvent;
		
		protected var oldTime:int = 0;
		override public function enterFrame():void {
			if (isDrag && parent) {
				if (isLockCenter) {
					var localPoint:Point = parent.globalToLocal(BtRun.mousePoint);
					set_x = Math.round(localPoint.x);
					set_y = Math.round(localPoint.y);
				} else {
					set_x += Math.round(BtRun.mouseMoveX);
					set_y += Math.round(BtRun.mouseMoveY);
				}
			}
			
			if (_isPlaying) {
				if (BtRun.nowTime - oldTime >= _ms) {
					oldTime = BtRun.nowTime;
				} else {
					return;
				}
				if (++_currentFrame > _totalFrames) _currentFrame = 1;
				
				tempBtFrame = _btFrameVec[_currentFrame-1];
				if (tempBtFrame != _currentBtFrame) {
					_currentBtFrame = tempBtFrame;
					render();
					updateXY();
				}
				
				if (isPlayOnce && (currentFrame==totalFrames)) {
					stop();
				}
			}
			
//			if (_mouseEnable) {
//				oldHit = nowHit;
//				nowHit = isMouseHit();
//				if (nowHit && !oldHit && settedMouseEvent) {
//					mouseEvent = new MouseEvent(MouseEvent.MOUSE_OVER);
//					dispatchEvent(mouseEvent);
//					dispatchEvent(new MouseEvent(MouseEvent.ROLL_OVER));
//				}
//				if (!nowHit && oldHit) {
//					mouseEvent = new MouseEvent(MouseEvent.MOUSE_OUT)
//					dispatchEvent(mouseEvent);
//					dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT));
//				}
//				//trace("oldHit:"+oldHit + " nowHit:"+nowHit + " oldDown:"+BtRun.oldMouseDown + " nowDown:"+BtRun.nowMouseDown);
//			}
			
		}
		
		override protected function render():void {
			_rect = _currentBtFrame.rect;
			bitmapData = _currentBtFrame.bitmapData;
		}
		
		public var settedMouseEvent:Boolean = false;
		
		public function setMouseEvent(e:MouseEvent, isMouseHit:Boolean, isPointHit:Boolean):void {
			if (_mouseEnable) {
				oldHit = nowHit;
				nowHit = isMouseHit;
				
				if (nowHit && !oldHit) {
					mouseEvent = new MouseEvent(MouseEvent.MOUSE_OVER);
					dispatchEvent(mouseEvent);
					dispatchEvent(new MouseEvent(MouseEvent.ROLL_OVER));
				} else if (!nowHit && oldHit) {
					mouseEvent = new MouseEvent(MouseEvent.MOUSE_OUT)
					dispatchEvent(mouseEvent);
					dispatchEvent(new MouseEvent(MouseEvent.ROLL_OUT));
					
				} else if (nowHit) {
					if (e.type == MouseEvent.DOUBLE_CLICK) {
						if (_doubleClickEnabled) {
							dispatchEvent(e);
						} else {
							dispatchEvent(new MouseEvent(MouseEvent.CLICK));
						}
					} else {
						dispatchEvent(e);
					}
				}
				
			}
		}
		
		public function registAction(action:String, frameArr:Array):void {
			if (BtDisplay.checkFrameArr(this, frameArr)) {
				action2FrameDic[action] = frameArr;
			}
		}
		
		public function unregistAction(action:String):void {
			action2FrameDic[action] = undefined;
		}
		
		public var playActionOnce:Boolean = false;
		public function playAction(action:String, $playActionOnce:Boolean=false, $actionFinishListener:Function=null, param:Object=null):void {
			if (action2FrameDic[action] == undefined) {
				trace("[BtAnimationX] playAction() 没有设置这个动作 " + action);
				return;
			}
			playActionOnce = $playActionOnce;
			actionFinishListener = $actionFinishListener;
			listenerParam = param;
			currentFrameArr = action2FrameDic[action].slice();
			currentAction = action;
			lastActionFrame = currentFrameArr[currentFrameArr.length - 1];
		}
		
//		public function applyFilter(bitmapFilter:BitmapFilter, destPoint:Point):void {
//			if (tempBtFrame == null) return; 
//				
//			var tempRect:Rectangle = tempBtFrame.rect.clone();
//			var tempData:BitmapData = new BitmapData(tempRect.width+destPoint.x*2, tempRect.height+destPoint.y*2, true, 0);
//			tempData.applyFilter(tempBtFrame.bitmapData, new Rectangle(0,0,tempRect.width,tempRect.height), destPoint, BtDisplay.highlightFilter);
//			tempRect.x -= destPoint.x;
//			tempRect.y -= destPoint.y;
//			_currentBtFrame = tempBtFrame = new BtFrame(tempData, tempRect);
//			render();
//			updateXY();
//		}
		
		public function isPointHit():Boolean {
			var localPoint:Point = globalToLocal(BtRun.mousePoint);
			return bitmapData.getPixel32(localPoint.x, localPoint.y) != 0;
		}
		
	}
}
